package fes.ui;

import com.ht.aems.fesmodel.FesPersistenceFactory;
import com.ht.aems.fesmodel.IUnitManager;
import com.ht.aems.fesmodel.data.MeasurePoint;
import com.ht.aems.swing.util.TreeUtil;
import org.javabuilders.swing.SwingJavaBuilder;

import javax.swing.*;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.MutableTreeNode;

/**
 * Created by IntelliJ IDEA.
 *
 * @author Dong Shufeng
 *         Date: 2010-8-17
 */
public class PointChooser extends JDialog {
    public static final int MODE_YC_ONLY = 0;
    public static final int MODE_YX_ONLY = 1;
    public static final int MODE_ALL = 2;
    MeasurePoint point;
    JTree tree;
    DefaultTreeModel treeModel;
    java.util.List<IUnitManager> roots;
    JPanel treePanel;
    String message = "";
    public PointChooser(int mode) {
        roots = FesPersistenceFactory.createPersistence().getRoots();
        DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode("");
        treeModel = new DefaultTreeModel(rootNode);
        for (IUnitManager r : roots) {
            rootNode.add((MutableTreeNode) new DefaultTreeModel(RTUTreeUtil.getDeviceTreeNode(r)).getRoot());
        }
        tree = new JTree(treeModel);
        tree.setRootVisible(false);
        TreeUtil.expandTree(tree, (DefaultMutableTreeNode) tree.getModel().getRoot(), 3);

        if (mode == MODE_YC_ONLY) {
            RTUTreeUtil.addAnalogPointToTree((DefaultMutableTreeNode) tree.getModel().getRoot());
        } else if (mode == MODE_YX_ONLY) {
            RTUTreeUtil.addDiscretePointToTree((DefaultMutableTreeNode) tree.getModel().getRoot());
        } else
            RTUTreeUtil.addPointToTree((DefaultMutableTreeNode) tree.getModel().getRoot());
        treePanel = TreeUtil.getSearchBarPanel(tree);

        SwingJavaBuilder.build(this);

        tree.addTreeSelectionListener(new TreeSelectionListener() {
            public void valueChanged(TreeSelectionEvent e) {
                if (e.getPath() == null || e.getPath().getLastPathComponent() == null) {
                    point = null;
                    message = "";
                    return;
                }
                DefaultMutableTreeNode node = (DefaultMutableTreeNode) e.getPath().getLastPathComponent();
                Object obj = node.getUserObject();
                if (obj instanceof MeasurePoint) {
                    point = (MeasurePoint) obj;
                    message = point.getName() + "(" + point.getId() + ")";
                } else {
                    point = null;
                    message = "";
                }
            }
        });
    }

    private void onOk() {
        dispose();
    }

    private void onCancel() {
        point = null;
        dispose();
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public MeasurePoint getPoint() {
        return point;
    }

    public static MeasurePoint getYCPoint() {
        PointChooser chooser = new PointChooser(MODE_YC_ONLY);
        chooser.setModal(true);
        chooser.setVisible(true);
        return chooser.getPoint();
    }

    public static MeasurePoint getYXPoint() {
        PointChooser chooser = new PointChooser(MODE_YX_ONLY);
        chooser.setModal(true);
        chooser.setVisible(true);
        return chooser.getPoint();
    }

    public static MeasurePoint getMeasurePoint() {
        PointChooser chooser = new PointChooser(MODE_ALL);
        chooser.setModal(true);
        chooser.setVisible(true);
        return chooser.getPoint();
    }
}
